/**
 * UserInfoAdapter.java
 */
package com.krui.iweibo.adapter;

import java.util.List;

import org.w3c.dom.Text;

import com.krui.iweibo.R;
import com.krui.iweibo.model.UserInfo;
import com.krui.iweibo.utils.ImageFactory;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * UserInfoAdapter.java
 * @author krui
 * 2012-6-26 上午07:46:23
 * 
 */
public class UserInfoAdapter extends BaseAdapter{
	
	private Context context;
	private List<UserInfo> users;

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	
	public UserInfoAdapter(Context context,List<UserInfo> users){
		this.context = context;
		this.users = users;
		
	}
	@Override
	public int getCount() {
		return users==null ? 0 : users.size();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public UserInfo getItem(int position) {
		return users.get(position);
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		return users.get(position).getId();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		final UserInfo user = getItem(position);
		
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.users_selected_templete, null);
			vh = new ViewHolder();
			vh.ivIcon = (ImageView)convertView.findViewById(R.id.img_user_head_temp);
			vh.tvName = (TextView)convertView.findViewById(R.id.txt_show_name);
			convertView.setTag(vh);
		}else {
			vh = (ViewHolder) convertView.getTag();
		}
		
		
		
		
		if (vh != null) {
			if (user.getUserIcon() != null) {
				vh.ivIcon.setImageBitmap(user.getUserIcon());
			}
			vh.tvName.setText(user.getUserName());
		}
		return convertView;
	}
	
	
	class ViewHolder{
		private ImageView ivIcon;
		private TextView tvName;
	}
	

}
